//
//  PolygonShape.m
//  WhatATool
//
//  Created by Lefty Fabulous on 8/20/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "PolygonShape.h"


@implementation PolygonShape

@synthesize numberOfSides, minimumNumberOfSides, maximumNumberOfSides;

- (id) init {
	self = [super init];
	if (self != nil) {
		defaultMax = 12;
		defaultMin = 3;
		return [self initWithNumberOfSides:4 minimumNumberOfSides:3 maximumNumberOfSides:10];
	}
	return self;
}

- (void) setNumberOfSides:(int) sides {
	if (sides < minimumNumberOfSides) {
		NSLog(@"Invalid number of sides: %i is less than minimum allowed (%i)", sides, minimumNumberOfSides);
		numberOfSides = minimumNumberOfSides;
	} else if (sides > maximumNumberOfSides) {
		NSLog(@"Invalid number of sides: %i is more than maximum allowed (%i)", sides, maximumNumberOfSides);
		numberOfSides = maximumNumberOfSides;
	} else {
		numberOfSides = sides;
	}
}

- (void) setMinimumNumberOfSides:(int) sides {
	if (sides <= 2) {
		NSLog(@"Invalid number of sides: %i is not greater than 2", sides);
		minimumNumberOfSides = defaultMin;
	} else {
		minimumNumberOfSides = sides;
	}
}

- (void) setMaximumNumberOfSides:(int) sides {
	if (sides > 12) {
		NSLog(@"Invalid number of sides: %i is greater than 12", sides);
		maximumNumberOfSides = defaultMax;
	} else {
		maximumNumberOfSides = sides;
	}
}

- (id) initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min 
	   maximumNumberOfSides:(int)max {
	if (self != nil) {
		[self setMinimumNumberOfSides:min];
		[self setMaximumNumberOfSides:max];
		[self setNumberOfSides:sides];
	}
	return self;
}

- (float) angleInDegrees {
	return (180 * 
			(numberOfSides - 2) / numberOfSides);
}

- (float) angleInRadians {
	return (M_PI / (180 / [self angleInDegrees]));
}

- (NSString *) name {
	switch (numberOfSides) {
		case 3:
			return @"Triangle";
		case 4:
			return @"Quadrangle";
		case 5:
			return @"Pentagon";
		case 6:
			return @"Hexagon";
		case 7:
			return @"Heptagon";
		case 8:
			return @"Octagon";
		case 9:
			return @"Nonagon";
		case 10:
			return @"Decagon";
		case 11:
			return @"Hendecagon";
		case 12:
			return @"Dodecagon";
		default:
			NSLog(@"Unexpected condition: PolygonShape cannot ever have more than 12 sides");
			return @"ERROR";
	}
}

- (NSString *) description {
	return [NSString stringWithFormat:@"Polygon with %i sides (you could say %@) with angles of %f degrees (%f radians).",
			[self numberOfSides], [self name], [self angleInDegrees], [self angleInRadians]];
}

- (void) dealloc
{
	NSLog(@"dealloc caled on PolygonShape %@", [self description]);
	[super dealloc];
}


@end
